<template>
    <div v-if="hasData" class="gztips-box">
        <!-- 岁运天干 -->
        <div class="gztipsbox-item">
            <div class="gztipsbox-item-label">岁运天干<span>：</span></div>
            <div class="gztipsbox-item-value">
                <span v-if="suiyunTiangan.length === 0">无</span>
                <template v-else>
                    <span v-for="(item, index) in suiyunTiangan" :key="index">
                        {{ item }}<span v-if="index < suiyunTiangan.length - 1"> | </span>
                    </span>
                </template>
            </div>
        </div>
        
        <!-- 岁运地支 -->
        <div class="gztipsbox-item">
            <div class="gztipsbox-item-label">岁运地支<span>：</span></div>
            <div class="gztipsbox-item-value">
                <span v-if="suiyunDizhi.length === 0">无</span>
                <template v-else>
                    <span v-for="(item, index) in suiyunDizhi" :key="index">
                        {{ item }}<span v-if="index < suiyunDizhi.length - 1"> | </span>
                    </span>
                </template>
            </div>
        </div>
        
        <!-- 岁运整柱 -->
        <div class="gztipsbox-item">
            <div class="gztipsbox-item-label">岁运整柱<span>：</span></div>
            <div class="gztipsbox-item-value">
                <span v-if="suiyunZhengzhu.length === 0">无</span>
                <template v-else>
                    <span v-for="(item, index) in suiyunZhengzhu" :key="index">
                        {{ item }}<span v-if="index < suiyunZhengzhu.length - 1"> | </span>
                    </span>
                </template>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { 
    calculateTianganRelations, 
    calculateDizhiRelations, 
    calculateZhengzhuRelations,
    calculateSinglePillarRelations,
    calculatePillarPairRelations
} from '@/utils/relation-utils'

interface Pillar {
    stem?: string
    branch?: string
}

interface BaziData {
    year?: Pillar
    month?: Pillar
    day?: Pillar
    hour?: Pillar
}

interface Props {
    decade?: Pillar      // 大运
    fortune?: Pillar     // 流年
    monthTime?: Pillar   // 流月
    dayTime?: Pillar     // 流日
    hourTime?: Pillar    // 流时
    showTaiMingShen?: boolean  // 是否显示胎身明（胎元、命宫、身宫）
    taiyuan?: Pillar     // 胎元
    minggong?: Pillar    // 命宫
    shengong?: Pillar    // 身宫
    baziData?: BaziData  // 原局四柱数据（年柱、月柱、日柱、时柱）
}

const props = defineProps<Props>()

// 构建原局四柱数组
const yuanjuPillars = computed(() => {
    const pillarsList: Array<{ gan: string; zhi: string }> = []
    
    // 添加原局四柱（年柱、月柱、日柱、时柱）
    if (props.baziData) {
        if (props.baziData.year?.stem && props.baziData.year?.branch) {
            pillarsList.push({ gan: props.baziData.year.stem, zhi: props.baziData.year.branch })
        }
        if (props.baziData.month?.stem && props.baziData.month?.branch) {
            pillarsList.push({ gan: props.baziData.month.stem, zhi: props.baziData.month.branch })
        }
        if (props.baziData.day?.stem && props.baziData.day?.branch) {
            pillarsList.push({ gan: props.baziData.day.stem, zhi: props.baziData.day.branch })
        }
        if (props.baziData.hour?.stem && props.baziData.hour?.branch) {
            pillarsList.push({ gan: props.baziData.hour.stem, zhi: props.baziData.hour.branch })
        }
    }
    
    return pillarsList
})

// 构建岁运柱数组
const suiyunPillars = computed(() => {
    const pillarsList: Array<{ gan: string; zhi: string }> = []
    
    // 如果开启了胎身明，使用胎元、命宫、身宫
    if (props.showTaiMingShen) {
        if (props.taiyuan?.stem && props.taiyuan?.branch) {
            pillarsList.push({ gan: props.taiyuan.stem, zhi: props.taiyuan.branch })
        }
        if (props.minggong?.stem && props.minggong?.branch) {
            pillarsList.push({ gan: props.minggong.stem, zhi: props.minggong.branch })
        }
        if (props.shengong?.stem && props.shengong?.branch) {
            pillarsList.push({ gan: props.shengong.stem, zhi: props.shengong.branch })
        }
    } else {
        // 否则使用大运、流年、流月、流日、流时
        if (props.decade?.stem && props.decade?.branch) {
            pillarsList.push({ gan: props.decade.stem, zhi: props.decade.branch })
        }
        if (props.fortune?.stem && props.fortune?.branch) {
            pillarsList.push({ gan: props.fortune.stem, zhi: props.fortune.branch })
        }
        if (props.monthTime?.stem && props.monthTime?.branch) {
            pillarsList.push({ gan: props.monthTime.stem, zhi: props.monthTime.branch })
        }
        if (props.dayTime?.stem && props.dayTime?.branch) {
            pillarsList.push({ gan: props.dayTime.stem, zhi: props.dayTime.branch })
        }
        if (props.hourTime?.stem && props.hourTime?.branch) {
            pillarsList.push({ gan: props.hourTime.stem, zhi: props.hourTime.branch })
        }
    }
    
    return pillarsList
})

// 构建所有柱数组（用于计算交叉关系）
const allPillars = computed(() => {
    return [...yuanjuPillars.value, ...suiyunPillars.value]
})

// 判断是否有数据
const hasData = computed(() => {
    return suiyunPillars.value.length > 0 || (yuanjuPillars.value.length > 0 && suiyunPillars.value.length > 0)
})

// 计算岁运天干关系（只计算岁运之间的关系，以及岁运和原局四柱之间的关系）
const suiyunTiangan = computed(() => {
    const relations: string[] = []
    
    // 1. 计算岁运之间的关系
    if (suiyunPillars.value.length > 0) {
        relations.push(...calculateTianganRelations(suiyunPillars.value))
    }
    
    // 2. 计算原局四柱和岁运之间的交叉关系
    if (yuanjuPillars.value.length > 0 && suiyunPillars.value.length > 0) {
        // 计算所有柱的关系，然后过滤掉原局四柱内部的关系
        const allRelations = calculateTianganRelations(allPillars.value)
        const yuanjuRelations = calculateTianganRelations(yuanjuPillars.value)
        const yuanjuRelationSet = new Set(yuanjuRelations)
        
        // 只保留包含岁运的关系（排除原局四柱内部的关系）
        const crossRelations = allRelations.filter(rel => !yuanjuRelationSet.has(rel))
        relations.push(...crossRelations)
    }
    
    return relations
})

// 计算岁运地支关系（只计算岁运之间的关系，以及岁运和原局四柱之间的关系）
const suiyunDizhi = computed(() => {
    const relations: string[] = []
    
    // 1. 计算岁运之间的关系
    if (suiyunPillars.value.length > 0) {
        relations.push(...calculateDizhiRelations(suiyunPillars.value))
    }
    
    // 2. 计算原局四柱和岁运之间的交叉关系
    if (yuanjuPillars.value.length > 0 && suiyunPillars.value.length > 0) {
        // 计算所有柱的关系，然后过滤掉原局四柱内部的关系
        const allRelations = calculateDizhiRelations(allPillars.value)
        const yuanjuRelations = calculateDizhiRelations(yuanjuPillars.value)
        const yuanjuRelationSet = new Set(yuanjuRelations)
        
        // 只保留包含岁运的关系（排除原局四柱内部的关系）
        const crossRelations = allRelations.filter(rel => !yuanjuRelationSet.has(rel))
        relations.push(...crossRelations)
    }
    
    return relations
})

// 计算岁运整柱关系（只计算岁运之间的关系，以及岁运和原局四柱之间的关系）
const suiyunZhengzhu = computed(() => {
    const relations: string[] = []
    const handled = new Set<string>()
    
    // 1. 单柱关系（盖头、截脚）：只计算岁运的盖头、截脚
    if (suiyunPillars.value.length > 0) {
        const suiyunSingleRelations = calculateSinglePillarRelations(suiyunPillars.value)
        suiyunSingleRelations.forEach(rel => {
            if (!handled.has(rel)) {
                relations.push(rel)
                handled.add(rel)
            }
        })
    }
    
    // 2. 两两柱关系（伏吟、反吟）：计算岁运之间的关系
    if (suiyunPillars.value.length > 0) {
        const suiyunPairRelations = calculatePillarPairRelations(suiyunPillars.value)
        suiyunPairRelations.forEach(rel => {
            if (!handled.has(rel)) {
                relations.push(rel)
                handled.add(rel)
            }
        })
    }
    
    // 3. 两两柱关系（伏吟、反吟）：计算原局四柱和岁运之间的交叉关系
    if (yuanjuPillars.value.length > 0 && suiyunPillars.value.length > 0) {
        // 计算所有柱的两两关系，然后过滤掉原局四柱内部的关系
        const allPairRelations = calculatePillarPairRelations(allPillars.value)
        const yuanjuPairRelations = calculatePillarPairRelations(yuanjuPillars.value)
        const yuanjuPairRelationSet = new Set(yuanjuPairRelations)
        
        // 只保留包含岁运的关系（排除原局四柱内部的关系）
        const crossPairRelations = allPairRelations.filter(rel => !yuanjuPairRelationSet.has(rel))
        crossPairRelations.forEach(rel => {
            if (!handled.has(rel)) {
                relations.push(rel)
                handled.add(rel)
            }
        })
    }
    
    return relations
})
</script>

<style scoped>
.gztips-box {
    margin: 16px;
    padding: 0;
}

.gztipsbox-item {
    display: flex;
    align-items: flex-start;
    margin-bottom: 12px;
}

.gztipsbox-item:last-child {
    margin-bottom: 0;
}

.gztipsbox-item-label {
    font-size: 14px;
    color: #64748b;
    font-weight: 500;
    min-width: 80px;
    flex-shrink: 0;
}

.gztipsbox-item-value {
    flex: 1;
    font-size: 14px;
    color: #1e293b;
    font-weight: 400;
    line-height: 1.6;
}
</style>

